package com.fourcitizens.playas

class TownException {
	String message
	Town town
}

class TownService {

	static transactional = true

	Town createTown(String provinceName, String townName) {
		def town = new Town(name : townName)
		def province = Province.findByName(provinceName)
		if (province) {
			province.addToTowns(town)

			if (!province.save(failOnError: true))
				throw new TownException(message : "Could not create the town", town)
		}

		return town
	}

	Town createTown(Province province, String townName) {
		return createTown(province.name, townName)
	}

	Town getTownInProvince(Province province, String townName) {

		def town = Town.createCriteria().list {
			and {
				eq('province',province)
				eq('name',townName)
			}
		}

		return town[0]
	}
}
